<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Akun_model extends CI_Model {
	
	
	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	function get_data_akun(){
		//Select table name
		$table_name = "AKUN";
		
		//Build contents query
		$this->db->select('*')->from($table_name);
		$this->db->where('STATUS',1);
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		$this->db->select('*')->from($table_name);
       	$this->db->where('STATUS',1);
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
	
		//Return all
		return $return;
	}
	
	// fungsi menambah akun (add)
	function tambah_akun($data)
	{
		$this->db->insert('AKUN', $data);
	}

        // fungsi mengubah data akun (edit)
	function ubah_akun($data, $id_akun)
	{
            $this->db->where('ID_AKUN', $id_akun);
            $this->db->update('AKUN', $data);
	}

        // fungsi menghapus data akun
	function hapus_akun($id_akun)
	{
            $this->db->where('ID_AKUN', $id_akun);
            $this->db->delete('AKUN');
	}
	
	//fungsi query untuk jqgrid
	function getAllGrid($start,$limit,$sidx,$sord,$where){
		$this->db->select('*, if(STATUS="1","Aktif","Tidak aktif") as STATUS2',false);
		$this->db->limit($limit);
		if($where != NULL)$this->db->where($where,NULL,FALSE);
		$this->db->order_by($sidx,$sord);
		$query = $this->db->get('AKUN',$limit,$start);

		return $query->result();
        }

        // fungsi untuk mendapatkan akun terakhir
	function getLast_akun(){
            $this->db->select_max('ID_AKUN');
            $this->db->from('AKUN');

            return $this->db->get();
        }

        // fungsi untuk mendapatkan akun berdasarkan id
        function get_akun_byID($id_akun){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->where('ID_AKUN', $id_akun);

            return $this->db->get();
        }

        // fungsi untuk mendapatkan akun berdasarkan kode
        function get_akun_byKode($kode_akun){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->where('KODE_AKUN', $kode_akun);

            return $this->db->get();
        }

        // fungsi untuk mendapatkan akun berdasarkan id parent
        function get_akun_byParent($id_parent){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->where('ID_PARENT', $id_parent);

            return $this->db->get();
        }

        // fungsi untuk mendapatkan akun berdasarkan nama
        function get_akun_byNama($nama){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->like('NAMA_AKUN', $nama);

            return $this->db->get();
        }

        // fungsi untuk mendapatkan akun berdasarkan kode
        function get_akun_induk(){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->where('STATUS_AKUN !=', 2);

            return $this->db->get();
        }

        // fungsi untuk mendapatkan semua akun yg statusny aktif
        function get_active_akun(){
            $this->db->select('*');
            $this->db->from('AKUN');
            $this->db->where('STATUS', 1);
            $this->db->where('STATUS_AKUN', 1);

            return $this->db->get();
        }
}